/*********************************************************************************
 *      Copyright:  (C) 2021 litupei<lismilepei@163.com>
 *                  All rights reserved.
 *
 *       Filename:  prj_cjson.h
 *    Description:  This file is prj_cjson.h
 *                 
 *        Version:  1.0.0(2021年08月09日)
 *         Author:  litupei <lismilepei@163.com>
 *      ChangeLog:  1, Release initial version on "2021年08月09日 17时23分47秒"
 *                 
 ********************************************************************************/

#include "prj_cjson.h"

/*封装数据csjon*/
char *pack_json(float temp)
{
	cJSON		*jsonroot = NULL;
	static char	*p = NULL;
	int		value = 1;

	jsonroot = cJSON_CreateObject();
	if(jsonroot == NULL)
	{
		printf("create cJSONroot obj failure! \n");
		return NULL;
	}
	printf("create cJSONroot obj successfully! \n");

	cJSON_AddStringToObject(jsonroot, "method", "thing.service.propert.set");
	cJSON_AddStringToObject(jsonroot, "id", "123456");

	cJSON   *subjson = NULL;
	subjson = cJSON_CreateObject();
	if(subjson == NULL)
	{
		cJSON_Delete(jsonroot);
		printf("create cJSONsub obj failure! \n");
		return NULL;
	}
	printf("create cJSONsub obj successfully! \n");

	cJSON_AddItemToObject(jsonroot, "params", subjson);
	cJSON_AddItemToObject(subjson, "cpu_temperature", cJSON_CreateNumber(temp));
	cJSON_AddItemToObject(subjson, "PowerLed", cJSON_CreateNumber(value));
	cJSON_AddStringToObject(jsonroot, "Version", "1.0.0");

	p = cJSON_Print(jsonroot);

	if(p == NULL)
	{
		printf("print cJSONroot failure! \n");
		cJSON_Delete(jsonroot);
		return NULL;
	}

	cJSON_Delete(jsonroot);

	return p;

}

//解析JSON
int parse_payload(char *sub_msg, int *led_value)
{
	int     led = 0;

    	printf("***************parse_payload message********************\n");
	if(sub_msg == NULL)
	{
		printf("the message pass failture :%s \n", strerror(errno));
		return -1;
	}
	
	cJSON *sub_json = cJSON_Parse(sub_msg);
	if(sub_json == NULL)
	{
		printf("cJSON_Parse() failture : %s! \n",strerror(errno));
		return -1;
	}

	cJSON *s_msg = cJSON_GetObjectItem(sub_json, "method");
	if(s_msg == NULL)
	{
		printf("get 'method' message failture : %s!", strerror(errno));
	}
	printf("'method' : %s \n", s_msg->valuestring);

	s_msg = cJSON_GetObjectItem(sub_json, "id");
	if(s_msg == NULL)
	{
		printf("get 'id' message failture : %s!", strerror(errno));
	}
	printf("'id' : %s \n", s_msg->valuestring);

	s_msg = cJSON_GetObjectItem(sub_json, "params");
	if(s_msg == NULL)
	{
		printf("get 'params' message failture : %s!", strerror(errno));
	}

	cJSON *ss_msg = cJSON_GetObjectItem(s_msg, "PowerLed");
	if(ss_msg == NULL)
	{
		printf("get 'PowerLed' message failture : %s!", strerror(errno));
	}
	printf("'PowerLed' : %d \n", ss_msg->valueint);

    	*led_value = ss_msg->valueint;

	s_msg = cJSON_GetObjectItem(sub_json, "version");
	if(s_msg == NULL)
	{
		printf("get 'version' message failture : %s!", strerror(errno));
	}
	printf("'version' : %s \n", s_msg->valuestring);
	printf("************************************************************\n");
    
	cJSON_Delete(s_msg);
	return 0;
}

